package com.WangNan.mall.service.impl;

import com.WangNan.mall.entity.Goods;
import com.WangNan.mall.mapper.GoodsMapper;
import com.WangNan.mall.pojo.GoodsQO;
import com.WangNan.mall.pojo.PageBean;
import com.WangNan.mall.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author :W.
 * @Date:2025/4/22 16:02
 */
@Service
public class GoodsServiceImpl implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void add(Goods goods){
        if (goods.getHottest() == null){
            goods.setHottest("n");
        }
        if (goods.getNewest() == null){
            goods.setNewest("n");
        }

        goods.setSaleTime(LocalDateTime.now());
        goods.setStatus("y");

        goodsMapper.insert(goods);
    }

//    @Override
//    public List<Goods> findAll(){
//        return goodsMapper.list();
//    }

    @Override
    public PageBean<Goods> findByPage(int pageNo, GoodsQO goodsQO) {
        PageBean<Goods> pageBean = new PageBean<>();
        //根据参数进行分页:第一个参数就是页码，第二个表示分页大小
        //对紧跟在这句代码之后的查询就会有分页效果
        PageHelper.startPage(pageNo,2);
        List<Goods> list = goodsMapper.list(goodsQO);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);

        pageBean.setData(pageInfo.getList());
        pageBean.setTotalPage(pageInfo.getPages());
        pageBean.setTotal((int)pageInfo.getTotal());
        pageBean.setPage(pageNo);
        pageBean.setPageSize(2);
        pageBean.setPageCount(pageInfo.getList().size());


        return pageBean;
    }
}
